<template>
  <el-card shadow="hover">
    <el-table :data="skuList" border v-loading="loading">
      <el-table-column
        type="index"
        label="序号"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        label="名称"
        prop="skuName"
        width="200"
      ></el-table-column>
      <el-table-column
        label="描述"
        prop="skuDesc"
        width="200"
      ></el-table-column>
      <el-table-column label="默认图片" width="150">
        <template v-slot="{ row }">
          <el-image :src="row.skuDefaultImg" lazy class="sku-img"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="重量(KG)"
        prop="weight"
        width="150"
      ></el-table-column>
      <el-table-column
        label="价格(元)"
        prop="price"
        width="150"
      ></el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template v-slot="{ row }">
          <el-button type="primary" :icon="ArrowDown" size="small"></el-button>
          <el-button type="warning" :icon="Edit" size="small"></el-button>
          <el-button
            type="info"
            :icon="InfoFilled"
            size="small"
            @click="showDrawer(row)"
          ></el-button>
          <el-button type="danger" :icon="Delete" size="small"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getSkuList"
      @current-change="getSkuList"
    />

    <el-drawer v-model="drawerVisible" :with-header="false">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="名称">
          {{ skuData.skuName }}
        </el-descriptions-item>
        <el-descriptions-item label="描述">
          {{ skuData.skuDesc }}
        </el-descriptions-item>
        <el-descriptions-item label="价格">
          {{ skuData.price }}
        </el-descriptions-item>
        <el-descriptions-item label="重量">
          {{ skuData.weight }}
        </el-descriptions-item>
        <el-descriptions-item label="图片列表"> xxx </el-descriptions-item>
      </el-descriptions>
    </el-drawer>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { ArrowDown, Edit, InfoFilled, Delete } from "@element-plus/icons-vue";
import { getSkuListApi } from "@/api/product/sku";
import type { SkuList, SkuItem } from "@/api/product/models/skuModels";

defineOptions({
  name: "SkuList",
});

// 2. 显示单个 Sku 数据
const drawerVisible = ref(false);
const skuData = ref<SkuItem>({
  price: 0,
  skuDefaultImg: "",
  skuDesc: "",
  skuImageList: [],
  skuName: "",
  weight: 0,
});

const showDrawer = (row: SkuItem) => {
  drawerVisible.value = true;
  skuData.value = row;
};

// 1. SkuList 数据展示

const skuList = ref<SkuList>([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const loading = ref(false);

const getSkuList = async () => {
  loading.value = true;
  const res = await getSkuListApi(currentPage.value, pageSize.value);
  skuList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

onMounted(getSkuList);
</script>

<style scoped lang="scss">
.sku-img {
  width: 100px;
  height: 100px;
}

:deep(.el-descriptions__label) {
  width: 80px;
}
</style>
